import { timeFormat, navigateTo, countDown } from '../../utils/util';
const data = {
    status:'0',
    over_time:'1506392110',       // 订单支付最后期限
    cur_time:'1506391810',        // 当前时间
    order_time:'',      // 下单时间
    store_name:'',      // 店铺名称
    menu_list:[         // 已点商品列表
        {
            name:"鸡腿堡套餐",
            num:"1",
            price:"22"
        },
        {
            name: "2个鸡肉卷",
            num: "1",
            price: "17"
        },
        {
            name: "汉堡+鸡米花+中可",
            num: "1",
            price: "15"
        }
    ],
    order_info:{
        order_num:"12154524574885",
        pay_way:"在线支付",
    },
    delivery_info:{
        address:"众利网络科技有限公司",
        name:"陶文",
        tel:"18629498676" 
    }
}
Page({
    data: {
        order_data:'',
        excessTime:''
    },
    onLoad: function (options) {
        const that = this;
        wx.showLoading({ title: '加载中' });
        // 模拟请求
        setTimeout(_=>{
            let over_time, cur_time, excessTime, obj;
                over_time = parseInt(data.over_time);
                cur_time = parseInt(data.cur_time);
                excessTime = over_time - cur_time;

                obj = this.format(excessTime);
                // 对数据进行加工
                switch (data.status) {
                    case '0':
                        data.status_txt = '等待支付';
                        data.status_tip='逾期未支付,订单将自动取消';
                        break;
                    case '1':
                        data.status_txt = '等待接单';
                        data.status_tip = '等待商家接收订单';
                        break;
                    case '2':
                        data.status_txt = '正在配送';
                        data.status_tip = '订单正在配送中';
                        break;
                    case '3':
                        data.status_txt = '配送完成';
                        data.status_tip = '订单配送完成';
                        break;
                    case '4':
                        data.status_txt = '订单取消';
                        data.status_tip = '系统取消了订单,理由是"超过15分钟为支付"';
                        break;
                }
               

            this.setData({ excessTime: obj, order_data:data });
            this.count_down(data.over_time, data.cur_time);
            wx.hideLoading();
        },600);

        
    },
    onReady: function () {
    
    },
    // 倒计时
    count_down:function(over_tiem,cur_time){
        const that = this;
        let excessTime = over_tiem - cur_time;

        this.timer = setInterval(_=>{
            excessTime--;
            let time = this.format(excessTime);
            // 满足条件移除定时器
            if(time.m=='00'&&time.s=="00"){
                clearInterval(this.timer);
            }

            that.setData({ excessTime:time });
        },1000);
    },
    // format
    format:function(time){
        let m,s,obj;
        m = parseInt(time/60);
        s = parseInt( time - (60*m) );

        m < 10 ? m = '0' + m : null;
        s < 10 ? s = '0' + s : null;

        obj={m:m,s:s};

        return obj;
    },
    navigateTo:function(e){
        const name = e.target.dataset.name;
        console.log(name);
        navigateTo(name);
    },
    // 取消订单
    cancel_order:function(){
        wx.showModal({
            title: '订单取消',
            content: '您确定要取消该订单吗?',
            success: function (res) {
                if (res.confirm) {
                    console.log('是的');
                    // 返回首页
                    navigateTo('index');
                } else if (res.cancel) {
                    console.log('不取消')
                }
            }
        })
    }
})
